package com.ruoyi.web.util;

import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.distance.DistanceUtils;
import com.spatial4j.core.shape.Rectangle;

/**
 * java类简单作用描述
 *
 * @Package: cn.fulong.bjwalk.findhome.util
 * @Author: yu_du_chen
 * @CreateDate: 2020/8/29 10:43
 * @Version: 1.0
 */
public class spatial {
    // 移动设备经纬度
    private double lon;
    private double lat;
    //千米
    final private static int radius = 16;

    public spatial() {
        this(116.311777,40.035986);
    }

    public spatial(double lon, double lat) {
        this.lon = lon;
        this.lat = lat;
        System.out.println("配置加载");
    }

    /**根据设置的范围 检索*/
    public void xyRange(){
        System.out.println(lon);
        System.out.println(lat);
        SpatialContext geo = SpatialContext.GEO;
        Rectangle rectangle = geo.getDistCalc().calcBoxByDistFromPt(
                geo.makePoint(lon, lat), radius * DistanceUtils.KM_TO_DEG, geo, null);
        System.out.println(rectangle.getMinX() + "-" + rectangle.getMaxX());// 经度范围
        System.out.println(rectangle.getMinY() + "-" + rectangle.getMaxY());// 纬度范围
    }

    /**商户经纬度和当前经纬度距离，传入商户经纬度*/
    public double xyDistance(double lon2,double lat2){
        SpatialContext geo = SpatialContext.GEO;
        double distance = geo.calcDistance(geo.makePoint(lon, lat), geo.makePoint(lon2, lat2))
                * DistanceUtils.DEG_TO_KM;
        return distance;
    }

    public static void main(String[] args) {
        spatial A = new spatial();
        A.xyRange();
        A.xyDistance(116.426612,39.905184);//北京站地铁站-A口
    }
}
